{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b3a6ae7-59e0-4498-814e-a93e1d634cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "%use fuel(2.3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ab39a624-cec1-4102-b23d-640c312b72dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "val envs = java.io.File(\"../../.env\")\n",
    "    .readLines()\n",
    "    .map {\n",
    "        it.split(\"=\")[0] to it.split(\"=\")[1].trim('\"')\n",
    "    }.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "575b8a31-6e1c-4edc-98f8-72c52992797b",
   "metadata": {},
   "outputs": [],
   "source": [
    "val session = envs.get(\"AOC_SESSION\")\n",
    "val year = 2021\n",
    "val day = 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd799fac-9c82-4c5a-b308-60691a01a866",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getInput(year: Int, day: Int, session: String): String {\n",
    "    val (_, _, result) = \"https://adventofcode.com/$year/day/$day/input\"\n",
    "    .httpGet()\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cac8a111-9146-49ea-8989-c6392869eaaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun submitAnswer(year: Int, day: Int, session: String, level: Int, answer: String): String {\n",
    "    val (_, _, result) = Fuel\n",
    "    .post(\n",
    "        \"https://adventofcode.com/$year/day/$day/answer\", \n",
    "        parameters = listOf(\"level\" to level, \"answer\" to answer))\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a064468c-9c51-4307-ba19-e3ff83c6c4ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "val sample = \"\"\"7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1\n",
    "\n",
    "22 13 17 11  0\n",
    " 8  2 23  4 24\n",
    "21  9 14 16  7\n",
    " 6 10  3 18  5\n",
    " 1 12 20 15 19\n",
    "\n",
    " 3 15  0  2 22\n",
    " 9 18 13 17  5\n",
    "19  8  7 25 23\n",
    "20 11 10 24  4\n",
    "14 21 16 12  6\n",
    "\n",
    "14 21 17 24  4\n",
    "10 16 15  9 19\n",
    "18  8 23 26 20\n",
    "22 11 13  6  5\n",
    " 2  0 12  3  7\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "878489a4-5e1e-4c04-a209-650185a8de67",
   "metadata": {},
   "outputs": [],
   "source": [
    "val input = getInput(year, day, session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f80e521a-bf11-4b36-85af-14f9111d262e",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun solve(input: String, nth: Int): String {\n",
    "    val lines = input.split('\\n')\n",
    "    val nums = lines[0].split(',').map(String::toInt)\n",
    "    val boards = (2 until lines.size step 6).map {\n",
    "        (it until it + 5).map {\n",
    "            lines[it].split(' ').filter{it.length > 0}.map(String::toInt)\n",
    "        }\n",
    "    }\n",
    "    val rank = if (nth >= 0) nth else boards.size + nth\n",
    "    val mp = nums.withIndex().map {\n",
    "        (index, num) -> num to index\n",
    "    }.toMap()\n",
    "    val winTurn = boards.map {\n",
    "        board -> (0 until 5).map {\n",
    "            i -> (0 until 5).map {\n",
    "                mp.getOrDefault(board[i][it], 0)\n",
    "            }.maxOf {it}\n",
    "            .coerceAtMost((0 until 5).map {\n",
    "                mp.getOrDefault(board[it][i], 0)\n",
    "            }.maxOf {it})\n",
    "        }.minOf {it} \n",
    "    }\n",
    "    \n",
    "    val boardOrderInWinTurn = (0 until boards.size).map {\n",
    "        Pair(winTurn[it], it)\n",
    "    }.toMutableList()\n",
    "    boardOrderInWinTurn.sortBy{it.first}\n",
    "    \n",
    "    val winnerNo = boardOrderInWinTurn[rank].second\n",
    "    val sumOfUnusedNumbers = boards[winnerNo].map {\n",
    "        it.filter {\n",
    "            mp.getOrDefault(it, 0) > winTurn[winnerNo]\n",
    "        }.sum()\n",
    "    }.sum()\n",
    "        \n",
    "    return (sumOfUnusedNumbers * nums[winTurn[winnerNo]]).toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "07df67e5-7a24-42e8-a6c4-f555675939df",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partOne(input: String): String = solve(input, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "eaa273a8-a166-44e0-9023-2449fecbbb94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4512"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partOne(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2ad99e07-5a96-40c4-873c-e72dd7c5f499",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49860"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partOneAns = partOne(input)\n",
    "partOneAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23679ce7-8258-41ae-9813-dff5745b482c",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 1, partOneAns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "528387cf-abb5-4da2-9fd3-9bbf865444a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partTwo(input: String) = solve(input, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b12a86b5-8d6f-4a6c-851a-af1d84ae168d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1924"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partTwo(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "62a5e94e-550f-42fe-9597-32e6f87a5b1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24628"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partTwoAns = partTwo(input)\n",
    "partTwoAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12e74919-3d56-4d88-9c51-bcb437ad3cf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 2, partTwoAns)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.6.20-dev-5432"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
